//
//  HJStatusCell.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/20.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit
import SnapKit

let HJStatusCellMargin:CGFloat = 10
let HJStatusCellHeadImgHW:CGFloat = 35
let HJStatusCellNameFont:CGFloat = 14
let HJStatusCellTimeFont:CGFloat = 11
let HJStatusCellContentFont:CGFloat = 14
let HJStatueCellToolBarBtnFont:CGFloat = 14
let HJStatusCellBottomViewH:CGFloat = 35
let HJStatusPictureItemMargin:CGFloat = 5
let HJStatusPictureItemWH:CGFloat = (ScreenWidth - 2 * HJStatusCellMargin - 2 * HJStatusPictureItemMargin) / 3

class HJStatusCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        updateUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func updateUI(){
        //设置背景颜色,originalview上面空出的一块就可以显示背景色,可以当做一条微博跟另一条微博的分割线
        backgroundColor = UIColor(white: 242/255, alpha: 1)
        //取消选中效果
        self.selectionStyle = .None
        //添加控件
        contentView.addSubview(originalview)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(retweetView)
        
        //约束控件
        originalview.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(contentView)
            //留出一块位置显示背景颜色,可以当做一条微博跟另一条微博的分割线
            make.top.equalTo(contentView).offset(HJStatusCellMargin)
//            make.height.equalTo(80)
        }
        statusToolBar.snp_makeConstraints { (make) -> Void in
            //取到该约束进行修改
            toolBarTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(retweetView)
            make.height.equalTo(HJStatusCellBottomViewH)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalview.snp_bottom)
            make.leading.trailing.equalTo(originalview)
//            make.height.equalTo(80)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(statusToolBar.snp_bottom)
            make.top.leading.trailing.equalTo(self)
        }
    }
    //定义一个约束类型(需要导入SnapKit头文件)
    var toolBarTopCons:Constraint?
    var statueViewModel:HJStatusViewModel? {
        didSet{
            originalview.statueViewModel = statueViewModel
            retweetView.statueViewModel = statueViewModel
            //先让约束失效才能update
            toolBarTopCons?.uninstall()
            
            if statueViewModel?.status?.retweeted_status != nil {
                retweetView.hidden = false
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    toolBarTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else{
                retweetView.hidden = true
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    toolBarTopCons = make.top.equalTo(originalview.snp_bottom).constraint
                })
            }
        }
    }
    //懒加载控件
    private lazy var originalview: HJOriginalStatusView = HJOriginalStatusView()
    private lazy var statusToolBar:HJStatusToolBar = HJStatusToolBar()
    private lazy var retweetView: HJRetweetView = HJRetweetView()
}
